from exts import db
from response import Response


class Book(db.Model):
    # 书籍的属性定义
    __tablename__ = 'book'
    book_id = db.Column('bookId', db.Integer, primary_key=True, autoincrement=True)
    title = db.Column('title', db.String(255), nullable=False)
    author = db.Column('author', db.String(255), nullable=False)
    isbn = db.Column('isbn', db.String(13), default="NULL")
    available = db.Column('available', db.Boolean, nullable=False, default=True)

    # 字典形式输出书籍信息
    def to_dict(self):
        return {
            'book_id': self.book_id,
            'title': self.title,
            'author': self.author,
            'isbn': self.isbn,
            'available': self.available,
            '_links': self.get_link()
        }

    # 获取书籍访问链接
    def get_link(self):
        return {
            'self': 'http://localhost:3000/books/{}'.format(self.book_id)
        }

    # 获取所有书籍信息
    @staticmethod
    def get_book():
        try:
            records = Book.query.all()
            records_list = [record.to_dict() for record in records]
            data = {
                'records': records_list,
                '_links': {
                    'self': 'http://localhost:3000/books'}
            }
            return Response.make_response(data=data, message='获取所有书籍记录成功！')
        except Exception as e:
            db.session.rollback()
            return Response.make_response(status='500', message=str(e))

    # 添加新书籍信息
    @staticmethod
    def add_book(title, author, isbn):
        book = Book(title=title, author=author, isbn=isbn)
        try:
            db.session.add(book)
            db.session.commit()
            data = {
                '_links': {
                    'self': 'http://localhost:3000/books/{}'.format(book.book_id)
                }
            }
            return Response.make_response(data=data, message='新图书添加成功！')
        except Exception as e:
            db.session.rollback()
            return Response.make_response(status='500', message=str(e))

    # 更新已有书籍信息
    @staticmethod
    def update_book(book_id, title, author, isbn, available):
        try:
            book = Book.query.get(book_id)
            if book is not None:
                book.title = title
                book.author = author
                book.isbn = isbn
                book.available = available
                db.session.commit()
                data = {
                    '_links': {
                        'self': 'http://localhost:3000/books/{}'.format(book_id)
                    }
                }
                return Response.make_response(data=data, message='图书信息更新成功！')
            else:
                return Response.make_response(status='404', message='未查询到图书！')
        except Exception as e:
            db.session.rollback()
            return Response.make_response(status='500', message=str(e))

    # 删除已有书籍信息
    @staticmethod
    def delete_book(book_id):
        try:
            book = Book.query.get(book_id)
            if book is not None:
                db.session.delete(book)
                db.session.commit()
                data = {
                    '_links': {
                        'self': 'http://localhost:3000/books/{}'.format(book_id)
                    }
                }
                return Response.make_response(data=data, message='图书删除成功！')
            else:
                return Response.make_response(status='404', message='未查询到图书！')
        except Exception as e:
            db.session.rollback()
            return Response.make_response(status='500', message=str(e))
